package com.lbc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbc.bean.dto.NewsDTO;
import com.lbc.bean.entity.NewsLike;
import com.lbc.bean.vo.NewsVO;
import com.lbc.common.resp.ResponseResult;
import com.lbc.common.utils.JwtUtils;
import com.lbc.service.NewsLikeService;
import com.lbc.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-03-17 18:31
 */
@Api(tags = "新闻接口")
@RestController
@RequestMapping("news")
public class NewsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsLikeService newsLikeService;


    @RequiresRoles("admin")
    @ApiOperation("添加新闻 , admin")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated NewsDTO newsDTO){
        return ResponseResult.toResponse(newsService.add(newsDTO));
    }

    @RequiresRoles("admin")
    @ApiOperation("编辑新闻 , admin")
    @PutMapping("modify")
    public ResponseResult modify(@RequestBody NewsDTO newsDTO){
        if (ObjectUtils.isEmpty(newsDTO.getNewsId())) {
            return ResponseResult.error("主键不能为空!");
        }
        return ResponseResult.toResponse(newsService.modify(newsDTO));
    }
    @RequiresRoles("admin")
    @ApiOperation("删除新闻 , admin")
    @DeleteMapping("/remove/{newsId}")
    public ResponseResult remove(@PathVariable("newsId") Integer newsId){
        return ResponseResult.toResponse(newsService.remove(newsId));
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseResult<PageInfo<NewsVO>> list(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(required = false) String searchKey){
        PageHelper.startPage(pageNum,pageSize);
        return new ResponseResult<>(new PageInfo<>(newsService.list(searchKey)));
    }
    @ApiOperation("随机列表")
    @GetMapping("/list/ramdon")
    public ResponseResult<PageInfo<NewsVO>> ramdon(){
        return new ResponseResult<>(new PageInfo<>(newsService.random()));
    }



    @ApiOperation("详情")
    @GetMapping("/{newsId}")
    public ResponseResult<NewsVO> list(@PathVariable Integer newsId){
        return new ResponseResult<>(newsService.get(newsId));
    }

    @ApiOperation("点赞")
    @GetMapping("/like/{newsId}")
    public ResponseResult like(@PathVariable("newsId") Integer newsId){
        NewsLike nl = newsLikeService.getByNewsIdAndUserId(newsId, JwtUtils.getUserId());
        if(ObjectUtils.isNotEmpty(nl)){
            return ResponseResult.error("已点过赞!");
        }
        return ResponseResult.toResponse(newsService.like(newsId));
    }

    @ApiOperation("增加浏览量")
    @GetMapping("/pageView/{newsId}")
    public ResponseResult pageView(@PathVariable("newsId") Integer newsId){
        return ResponseResult.toResponse(newsService.pageView(newsId));
    }
}

